package com.michaelliu.study.viewpager2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CompoundButton
import androidx.viewpager2.widget.ViewPager2
import com.michaelliu.study.viewpager2.databinding.ActivityVerticalViewPagerBinding

/**
 * 垂直的 ViewPager2
 */
class VerticalViewPagerActivity : AppCompatActivity(), CompoundButton.OnCheckedChangeListener {

    private lateinit var mBinding: ActivityVerticalViewPagerBinding
    private val translateX get() = mBinding.viewPager.orientation == ViewPager2.ORIENTATION_VERTICAL &&
            mBinding.cbTranslate.isChecked
    private val translateY get() = mBinding.viewPager.orientation == ViewPager2.ORIENTATION_HORIZONTAL &&
            mBinding.cbTranslate.isChecked


    private var mAnimation = ViewPager2.PageTransformer { page, position ->
        val absPos = Math.abs(position)
        page.apply {
            rotation = if (mBinding.cbRotate.isChecked) position * 360 else 0f
            translationY = if (translateY) absPos * 500f else 0f
            translationX = if (translateX) absPos * 350f else 0f
            if (mBinding.cbScale.isChecked) {
                val scale = if (absPos > 1) 0F else 1 - absPos
                scaleX = scale
                scaleY = scale
            } else {
                scaleX = 1f
                scaleY = 1f
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityVerticalViewPagerBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        mBinding.cbHorizontal.setOnCheckedChangeListener(this)
        mBinding.cbVertical.setOnCheckedChangeListener(this)
        mBinding.cbUserInput.setOnCheckedChangeListener(this)
        mBinding.cbSmooth.setOnCheckedChangeListener(this)
        mBinding.cbRotate.setOnCheckedChangeListener(this)
        mBinding.cbTranslate.setOnCheckedChangeListener(this)
        mBinding.cbScale.setOnCheckedChangeListener(this)

        mBinding.viewPager.adapter = ViewPagerAdapter(this)
        mBinding.viewPager.setPageTransformer(mAnimation)

        if (mBinding.viewPager.orientation == ViewPager2.ORIENTATION_VERTICAL) {
            mBinding.cbVertical.isChecked = true
            mBinding.cbHorizontal.isChecked = false
        } else {
            mBinding.cbVertical.isChecked = false
            mBinding.cbHorizontal.isChecked = true
        }
        mBinding.cbUserInput.isChecked = !mBinding.viewPager.isUserInputEnabled
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        when (buttonView?.id) {
            R.id.cb_horizontal -> {
                if (isChecked) {
                    mBinding.viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
                    mBinding.cbVertical.isChecked = false

                }
            }

            R.id.cb_vertical -> {
                if (isChecked) {
                    mBinding.viewPager.orientation = ViewPager2.ORIENTATION_VERTICAL
                    mBinding.cbHorizontal.isChecked = false

                }
            }

            R.id.cb_user_input -> {
                mBinding.viewPager.isUserInputEnabled = !isChecked
            }

            R.id.cb_smooth -> {

            }

            R.id.cb_rotate -> {

            }

            R.id.cb_translate -> {

            }

            R.id.cb_scale -> {

            }
        }
    }
}
